﻿using System.Collections.Generic;
using ePlanet.SingleWell.Services;
using ePlanet.SingleWell.DataAccess;
using System.Data;
using ePlanet.SingleWell.Models;
using System;

namespace ePlanet.SingleWell.Business
{
    public class BUSAttachment : IAttachmentService
    {
        private AttachmentDAL m_AttachmentDAL = null;
        private string m_DBHelperString = string.Empty;

        /// <summary>
        /// 初始化相应的对象
        /// </summary>
        /// <param name="DBHelperString"></param>
        public void Initialize(string DBHelperString)
        {
            if (string.IsNullOrEmpty(DBHelperString))
            {
                throw new Exception("构造函数的参数不能为空");
            }
            this.m_DBHelperString = DBHelperString;
            if (this.m_AttachmentDAL == null)
            {
                this.m_AttachmentDAL = new AttachmentDAL(DBHelperString);
            }
        }

        /// <summary>
        /// 保存附件，仅仅保存业务数据和附件的关联。（因为附件此时已经上传）
        /// </summary>
        /// <param name="dt_Attach"></param>
        /// <param name="objectTableName"></param>
        /// <param name="objectID"></param>
        /// <returns></returns>
        public bool SaveAttach(DataTable dt_Attach, string objectTableName, string objectID)
        {
            return this.m_AttachmentDAL.SaveAttach(dt_Attach, objectTableName, objectID);
        }

        /// <summary>
        /// 通过附件ID查询附件信息
        /// </summary>
        /// <param name="attachmentID"></param>
        /// <returns></returns>
        public AttachmentModel GetAttachmentModelByAttachmentID(string attachmentID)
        {
            AttachmentModel rtnAttachmentModel = null;
            DataTable oDataTable = this.m_AttachmentDAL.GetAttachmentByAttachmentID(attachmentID);
            if (oDataTable != null && oDataTable.Rows.Count > 0)
            {
                rtnAttachmentModel = this.TransDataRowToAttachmentModel(oDataTable.Rows[0]);
            }

            return rtnAttachmentModel;
        }

        /// <summary>
        /// 通过业务表的关联字段，查询业务表的所有附件信息
        /// </summary>
        /// <param name="objectID"></param>
        /// <returns></returns>
        public List<AttachmentModel> GetAttachmentModelByObjectID(string objectID)
        {
            List<AttachmentModel> rtnAttachmentModelList = null;
            DataTable oDataTable = this.m_AttachmentDAL.GetAttachmentListByObjectID(objectID);
            if (oDataTable != null && oDataTable.Rows.Count > 0)
            {
                rtnAttachmentModelList = new List<AttachmentModel>();
                foreach (DataRow dr in oDataTable.Rows)
                {
                    rtnAttachmentModelList.Add(this.TransDataRowToAttachmentModel(dr));
                }
            }

            return rtnAttachmentModelList;
        }

        /// <summary>
        /// 通过业务表的关联字段，查询业务表的所有附件信息
        /// </summary>
        /// <param name="objectIDList"></param>
        /// <returns></returns>
        public List<AttachmentModel> GetAttachmentModelByObjectID(List<string> objectIDList)
        {
            List<AttachmentModel> rtnAttachmentModelList = null;
            DataTable oDataTable = this.m_AttachmentDAL.GetAttachmentListByObjectID(objectIDList);
            if (oDataTable != null && oDataTable.Rows.Count > 0)
            {
                rtnAttachmentModelList = new List<AttachmentModel>();
                foreach (DataRow dr in oDataTable.Rows)
                {
                    rtnAttachmentModelList.Add(this.TransDataRowToAttachmentModel(dr));
                }
            }

            return rtnAttachmentModelList;
        }

        /// <summary>
        /// 转换Dr为附件对象
        /// </summary>
        /// <param name="dr"></param>
        /// <returns></returns>
        private AttachmentModel TransDataRowToAttachmentModel(DataRow dr)
        {
            if (dr == null)
            {
                throw new Exception("需要的转换的元数据为空");
            }

            AttachmentModel rtnAttachmentModel = new AttachmentModel();
            rtnAttachmentModel.AttachmentID = dr["ATTACHMENTID"].ToString();
            rtnAttachmentModel.AttachmentName = dr["ATTACHMENTNAME"].ToString();
            rtnAttachmentModel.AttachmentSize = Convert.ToInt64(dr["ATTACHMENTSIZE"]);
            rtnAttachmentModel.DBHelperString = this.m_DBHelperString;
            rtnAttachmentModel.DocumentType = dr["DOCUMENTTYPE"].ToString();
            rtnAttachmentModel.ObjectTableName = dr["OBJECTTABLENAME"].ToString();
            DateTime updateTime = DateTime.MinValue;
            if (DateTime.TryParse(dr["UPLOADTIME"].ToString(), out updateTime))
            {
                rtnAttachmentModel.UpdateTime = updateTime;
            }
            return rtnAttachmentModel;
        }
    }
}